#ifndef _GRO_RENDERER_SPRIG_H__
#define _GRO_RENDERER_SPRIG_H__

/*
GRO_GUI backend: SPriG

GRO_LineH
GRO_LineHBlend
GRO_LineV
GRO_LineVBlend
GRO_Line
GRO_LineBlend

GRO_Rect
GRO_RectBlend
GRO_RectFilled
GRO_RectFilledBlend
GRO_Circle
GRO_CircleBlend
GRO_CircleFilled
GRO_CircleFilledBlend
//GRO_Ellipse
//GRO_EllipseBlend
GRO_EllipseFilled
GRO_EllipseFilledBlend

GRO_TrigonFade
GRO_SetClip
GRO_DirtyAdd
GRO_RectIntersect

*/


#include "sprig.h"

namespace GRO
{
    
    class GRO_Image_SDL : public GRO_Image
    {
        public:
        SDL_Surface* surface;
    };
    
    class Renderer_Sprig : public Renderer
    {
        public:
        SDL_Surface* screen;
        Renderer_Sprig()
            : Renderer()
            , screen(NULL)
        {}
        Renderer_Sprig(SDL_Surface* screen)
            : Renderer()
        {
            this->screen = screen;
            if(screen != NULL && screen->format != NULL)
                format = *(screen->format);
        }
        
        virtual void Blit(GRO_Image* image, SDL_Rect* s, SDL_Rect* d)
        {
            SDL_BlitSurface(dynamic_cast<GRO_Image_SDL*>(image)->surface, s, screen, d);
        }
        
        virtual void Pixel(Sint16 x, Sint16 y, Uint32 color)
        {
            SPG_Pixel(screen, x, y, color);
        }

        virtual void LineH(Sint16 x1, Sint16 y, Sint16 x2, Uint32 color)
        {
            SPG_LineH(screen, x1, y, x2, color);
        }

        virtual void LineHBlend(Sint16 x1, Sint16 y, Sint16 x2, Uint32 color, Uint8 alpha)
        {
            SPG_LineHBlend(screen, x1, y, x2, color, alpha);
        }

        virtual void LineV(Sint16 x, Sint16 y1, Sint16 y2, Uint32 color)
        {
            SPG_LineV(screen, x, y1, y2, color);
        }

        virtual void LineVBlend(Sint16 x, Sint16 y1, Sint16 y2, Uint32 color, Uint8 alpha)
        {
            SPG_LineVBlend(screen, x, y1, y2, color, alpha);
        }

        virtual void Line(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color)
        {
            SPG_Line(screen, x1, y1, x2, y2, color);
        }

        virtual void LineBlend(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color, Uint8 alpha)
        {
            SPG_LineBlend(screen, x1, y1, x2, y2, color, alpha);
        }

        virtual void LineBlendAA(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color, Uint8 alpha)
        {
            SPG_PushAA(true);
            SPG_LineBlend(screen, x1, y1, x2, y2, color, alpha);
            SPG_PopAA();
        }




        virtual void Rect(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color)
        {
            SPG_Rect(screen, x1, y1, x2, y2, color);
        }

        virtual void RectBlend(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color, Uint8 alpha)
        {
            SPG_RectBlend(screen, x1, y1, x2, y2, color, alpha);
        }

        virtual void RectFilled(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color)
        {
            SPG_RectFilled(screen, x1, y1, x2, y2, color);
        }

        virtual void RectFilledBlend(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color, Uint8 alpha)
        {
            SPG_RectFilledBlend(screen, x1, y1, x2, y2, color, alpha);
        }


                
        // Colors in CCW order
        virtual void RectFade(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 c1, Uint32 c2, Uint32 c3, Uint32 c4)
        {
            SPG_Point points[] = {{x1,y1}, {x1,y2}, {x2,y2}, {x2,y1}};
            Uint32 colors[] = {c1, c2, c3, c4};
            SPG_PolygonFade(screen, 4, points, colors);
        }

        virtual void RectRound(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 radius, Uint32 color)
        {
            SPG_RectRound(screen, x1, y1, x2, y2, radius, color);
        }

        virtual void RectRoundFilledBlend(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 radius, Uint32 color, Uint8 alpha)
        {
            SPG_RectRoundFilledBlend(screen, x1, y1, x2, y2, radius, color, alpha);
        }

        virtual void ArcBlend(Sint16 x, Sint16 y, Sint16 radius, float startAngle, float endAngle, Uint32 color, Uint8 alpha)
        {
            if(SPG_GetRadians())
            {
                startAngle *= RADPERDEG;
                endAngle *= RADPERDEG;
            }
            SPG_ArcBlend(screen, x, y, radius, startAngle, endAngle, color, alpha);
        }


        virtual void Circle(Sint16 x, Sint16 y, Sint16 r, Uint32 color)
        {
            SPG_Circle(screen, x, y, r, color);
        }

        virtual void CircleBlend(Sint16 x, Sint16 y, Sint16 r, Uint32 color, Uint8 alpha)
        {
            SPG_CircleBlend(screen, x, y, r, color, alpha);
        }

        virtual void CircleFilled(Sint16 x, Sint16 y, Sint16 r, Uint32 color)
        {
            SPG_CircleFilled(screen, x, y, r, color);
        }

        virtual void CircleFilledBlend(Sint16 x, Sint16 y, Sint16 r, Uint32 color, Uint8 alpha)
        {
            SPG_CircleFilledBlend(screen, x, y, r, color, alpha);
        }

        virtual void EllipseFilled(Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color)
        {
            SPG_EllipseFilled(screen, x, y, rx, ry, color);
        }

        virtual void EllipseFilledBlend(Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color, Uint8 alpha)
        {
            SPG_EllipseFilledBlend(screen, x, y, rx, ry, color, alpha);
        }

        virtual void TrigonFade(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color1, Uint32 color2, Uint32 color3)
        {
            SPG_TrigonFade(screen, x1, y1, x2, y2, x3, y3, color1, color2, color3);
        }




        virtual SDL_Rect GetClip()
        {
            if(screen != NULL)
                return screen->clip_rect;
            SDL_Rect r = {0, 0, 0, 0};
            return r;
        }
        
        virtual void SetClip(SDL_Rect& clip)
        {
            SPG_SetClip(screen, clip);
        }

        virtual void DirtyAdd(SDL_Rect* rect)
        {
            if(SPG_DirtyEnabled())
                SPG_DirtyAdd(rect);
        }


        virtual void RectIntersect(const SDL_Rect& A, const SDL_Rect& B, SDL_Rect& intersection)
        {
            SPG_RectAND(A, B, &intersection);
        }

        virtual void PushThickness(int thickness)
        {
            SPG_PushThickness(thickness);
        }

        virtual int PopThickness()
        {
            return SPG_PopThickness();
        }
        
    };


}


#endif
